#!/bin/bash

echo "==> Polling DB…"

if [ -z "$1" ] || [ "$1" == "mysql" ]; then
  appdir=$(cd $(dirname "$0")/.. && pwd)
  [ -f /etc/app-env ] || exec "$appdir/script/docker-environment" $0 $@

  for i in {1..60}; do
    echo "... Checking for connection"
    nc -z -w5 db 3306 && exit
    sleep 1
  done

  echo "Unable to reach database!"
  exit 1
fi
